<?php
namespace app\admin\controller;
use think\Controller;
use sand\handle;
use app\admin\model\Payment as PaymentModel;
use app\admin\controller\Base;
/**
* 
*/
class Payment extends Base
{
    public function index(){
    	$this->assign('payments',model("Payment")->paginate(10));
        return $this->fetch();
    }

    public function pay(){
    	$id=intval(input('param.id'));
    	$payment=model('Payment')->get($id);
    	$userAcc=model('user_account')->get(['uid'=>$payment->uid]);
    	$this->assign('userAcc',$userAcc);
    	$this->assign('payment',$payment);
    	return $this->fetch();
    }

    public function dopay(){
        $id=intval(input('param.id'));
        $payment=model('Payment')->get($id);
        if($payment->status==1){
            $this->error("重复付款");
        }
        $userAcc = model('user_account')->get(['uid'=>$payment->uid])->getData();
        $productId='00000004';
        $params = require_once CONF_PATH.'sand/config/params.php';
        $AgentPay=$params['AgentPay'];
        $AgentPay['pt']['productId']=$productId;
        $AgentPay['pt']['tranAmt']=sprintf("%012d", $payment->amount*100);
        $AgentPay['pt']['accType']=$userAcc['acc_type'];
        $AgentPay['pt']['accNo']=$userAcc['acc_no'];
        $AgentPay['pt']['accAttr']=$userAcc['acc_attr'];
        $AgentPay['pt']['accName']=$userAcc['acc_name'];
        //dump($AgentPay);exit;
        $cert = require_once CONF_PATH.'sand/config/cert.php';
        $handle = new handle($cert);
        $res=json_decode($handle->execute($AgentPay));
        if($res->respCode == '0000'&&$res->resultFlag == '0'){
            $res->productId=$productId;
            $payment->pay_time= time();
            $payment->status = 1;
            if(model('order')->saveByRes($res)&&$payment->save())
                $this->success("操作成功");
            else $this->error("操作失败");
        }else{
            $this->error($res->respDesc);
        }
    }

    public function add(){
        return $this->fetch();
    }
    public function doadd(){
        if($_POST['phone'])
            $uid = model('user_account')->getFieldByPhone($_POST['phone'],'uid');
        else
            $this->error("手机号不能为空");
        $validate=validate('Payment');
        if(!$validate->scene('add')->check(input('post.')))
            $this->error($validate->getError()); 
        $amount=intval(input('post.amount'));
        $deadline=strtotime(input('post.deadline'));
        $payment = new PaymentModel();
        $payment->uid =$uid;
        $payment->amount =$amount;
        $payment->deadline =$deadline;
        $payment->create_time =time();
        if($payment->save())
            $this->success("增加成功");
        else
            $this->error("增加失败");
    }
    public function verify(){
        $phone = $_POST['phone'];
        $verify = model('user_account')->get(['phone'=>$phone]);
        if($verify){
            $json_verify['verify'] = $verify;
            $json_verify['status']=1;
        }else{
            $json_verify['status']=0;
        }
        $json_verify = json_encode($json_verify);
        echo $json_verify;
    }
}

